package com.zhiche.lisa.lspm.scheduled;


import com.zhiche.lisa.core.enums.DriverCheckinStatusEnum;
import com.zhiche.lisa.lspm.dao.model.driver.DriverCheckin;
import com.zhiche.lisa.lspm.service.driver.IDriverCheckinService;
import com.zhiche.lisa.lspm.service.weiXin.WeiXinExportService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务 controller
 *
 * @auther lsj
 * @create 2018-06-09
 */
@Component
public class DriverScheduled {

    private static final Logger LOGGER = LoggerFactory.getLogger(DriverScheduled.class);

    @Autowired
    private IDriverCheckinService driverCheckinService;
    @Autowired
    private WeiXinExportService weixinExportService;

    /**
     * 司机报班信息状态更新job
     * 处理3天前报班信息(每小时执行一次)
     */
//    @Scheduled(cron = "0 0 0/1 * * ?")
    public void driverCheckinDealWith() {
        LOGGER.info("QuartzController.driverCheckinDealWith start --->");
        try {
            List<DriverCheckin> list = driverCheckinService.getDriverCheckin36HoursNoTenant();
            if (CollectionUtils.isNotEmpty(list)) {
                list.forEach(driverCheckin -> {
                    LOGGER.info("QuartzController.driverCheckinDealWith-更新失效报班id:" + driverCheckin.getId());
                    driverCheckinService.updateCheckinStatus(driverCheckin.getId(), DriverCheckinStatusEnum.FAILURE.getCode());
                });
            }
            LOGGER.info("QuartzController.driverCheckinDealWith end --->");
        } catch (Exception ex) {
            LOGGER.info("QuartzController.driverCheckinDealWith Error : {}", ex.getMessage());
        }
    }

    /**
     * 获取微信公众号使用率
     */
    @Scheduled(cron = "40 0 0 * * ?")
//    @Scheduled(cron = "0 0/2 * * * ?")
    public void getWeChatPublicNumberUsageRate() {
        LOGGER.info("QuartzController.insertERPRate start --->");
        try {
            weixinExportService.insertERPRate();
        } catch (Exception ex) {
            LOGGER.info("QuartzController.insertERPRate Error : {}", ex.getMessage());
        }
    }
}
